package com.lingjtx.common.redis.topic;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class RedisExpiredMessage implements MessageListener {

    private final Map<String, RedisExpiredListener> expiredListenerMap;

    public RedisExpiredMessage(Map<String, RedisExpiredListener> expiredListenerMap) {
        this.expiredListenerMap = expiredListenerMap;
    }

    public RedisExpiredListener getHandler(String beanName) {
        return expiredListenerMap.get(beanName); // 可按 beanName 获取
    }

    private void executeAll(String Channel, String body) {
        for (RedisExpiredListener expiredListener : expiredListenerMap.values()) {
            expiredListener.onMessage(Channel, body);
        }
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        executeAll(new String(message.getChannel()), new String(message.getBody()));
    }
}
